package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jsbd.boss.dao.IScriptCosttimeRecordDao;
import org.jsbd.boss.domian.ScriptCosttimeRecord;
import org.jsbd.boss.service.IScriptCosttimeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("scriptCosttimeRecordService")
public class ScriptCosttimeRecordImpl implements IScriptCosttimeRecordService{
	
	@Autowired
	private IScriptCosttimeRecordDao scriptCosttimeRecordDao;

	@Override
	public Map<String, Object> getListPage(ScriptCosttimeRecord scriptCosttimeRecord) {
		scriptCosttimeRecord.setTaskType(1);
		List<ScriptCosttimeRecord> newRecords = scriptCosttimeRecordDao.getListPage(scriptCosttimeRecord);
		scriptCosttimeRecord.setTaskType(2);
		List<ScriptCosttimeRecord> retentRecords = scriptCosttimeRecordDao.getListPage(scriptCosttimeRecord);
		for (ScriptCosttimeRecord newRecord : newRecords) {
			Iterator<ScriptCosttimeRecord> it = retentRecords.iterator();
			while(it.hasNext()) {
				ScriptCosttimeRecord next = it.next();
				if (newRecord.getMark().equals(next.getMark()) && newRecord.getDay().equals(next.getDay())) {
					newRecord.setRetentCountAvg(next.getCountAvg());
					newRecord.setRetentUserAvg(next.getUserAvg());
					it.remove();
				}
			}
		}
		for (ScriptCosttimeRecord retentRecord : retentRecords) {
			retentRecord.setRetentCountAvg(retentRecord.getCountAvg());
			retentRecord.setRetentUserAvg(retentRecord.getUserAvg());
			retentRecord.setUserAvg(null);
			retentRecord.setCountAvg(null);
			newRecords.add(retentRecord);
		}
		
	    Collections.sort(newRecords, new Comparator<ScriptCosttimeRecord>() {
		   
            @Override
            public int compare(ScriptCosttimeRecord o1, ScriptCosttimeRecord o2) {
                if (Integer.parseInt(o1.getDay()) < Integer.parseInt(o2.getDay())) {
                    return 1;
                }
                if (Integer.parseInt(o1.getDay()) == Integer.parseInt(o2.getDay())) {
                    return 0;
                }
                return -1;
            }
        });
	    
		Map<String, Object> result = new HashMap<>();
		int len = newRecords.size();
		if(scriptCosttimeRecord.getPageInfo().getCurrentRow()>=len){
			result.put("total", 0);
			result.put("rows", new ArrayList<ScriptCosttimeRecord>(0));
			return result;
		}
		
		if(scriptCosttimeRecord.getPageInfo().getCurrentRow() + scriptCosttimeRecord.getPageInfo().getRows()<len){
			len = scriptCosttimeRecord.getPageInfo().getCurrentRow() + scriptCosttimeRecord.getPageInfo().getRows();
		}
		
		List<ScriptCosttimeRecord> showList = newRecords.subList(scriptCosttimeRecord.getPageInfo().getCurrentRow(), len);
		
		
		result.put("rows", showList);
		result.put("total", newRecords.size());
		return result;
	}
    
}
